package com.gchong3068.framework.common.response;

import com.gchong3068.framework.common.exception.BaseExceptionInterface;
import com.gchong3068.framework.common.exception.BizException;
import lombok.Data;

import java.io.Serializable;

/**
 *
 * @Auther: gchong3068
 * @Date: 2024/05/04 19:20
 * @return null
 **/
@Data
public class Response<T> implements Serializable {

    private boolean success = true;

    private String message;

    private String errorCode;

    private T data;



    public static <T> Response<T> success(){
        return new Response<>();
    }

    public static <T> Response<T> success(T data){
        Response<T> response = new Response<>();
        response.setData(data);
        return response;
    }


//    ======================================失败=================================
    public static <T> Response<T> fail(){
        Response<T> response = new Response<>();
        response.setSuccess(false);
        return response;
    }


    public static <T> Response<T> fail(String errorMessage){
        Response<T> response = new Response<>();
        response.setSuccess(false);
        response.setMessage(errorMessage);
        return response;
    }

    public static <T> Response<T> fail(String errorCode, String errorMessage){
        Response<T> response = new Response<>();
        response.setSuccess(false);
        response.setMessage(errorMessage);
        response.setErrorCode(errorCode);
        return response;
    }

    public static <T> Response<T> fail(BizException bizException){
        Response<T> response = new Response<>();
        response.setSuccess(false);
        response.setErrorCode(bizException.getErrorCode());
        response.setMessage(bizException.getErrorMessage());
        return response;
    }

    public static <T> Response<T> fail(BaseExceptionInterface baseExceptionInterface) {
        Response<T> response = new Response<>();
        response.setSuccess(false);
        response.setErrorCode(baseExceptionInterface.getErrorCode());
        response.setMessage(baseExceptionInterface.getErrorMessage());
        return response;
    }

}
